package com.yvon.maple.cloud.web.data.support;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yvon.maple.cloud.web.filter.LoginUserUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @author Yufeng
 */
@Slf4j
@AllArgsConstructor
public class DefaultMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime localDateTime = LocalDateTime.now();
        Long userId = this.getUserId();
        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class, localDateTime);
        this.strictInsertFill(metaObject,"updateTime",LocalDateTime.class, localDateTime);
        this.strictInsertFill(metaObject, "updateUserId", Long.class,  userId);
        this.strictInsertFill(metaObject,"createUserId",Long.class,  userId);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = this.getUserId();
        this.strictInsertFill(metaObject, "updateUserId", Long.class,  userId);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }

    public Long getUserId() {
        return LoginUserUtil.getUserId();
    }
}
